<?php
/** 
 *
 * mysql class
 * 定义了大部分SQL操作方法
 *
 */
	
class mysql {

	private $host;          //主机名
	private $name;          //数据库用户名
	private $pass;          //数据库密码
	private $table;         //表名
	
	function __construct($host,$name,$pass,$table){
		$this->host=$host;
		$this->name=$name;
		$this->pass=$pass;
		$this->table=$table;
		$this->connect();
	}

	function connect(){
		$conn = mysql_connect($this->host,$this->name,$this->pass) or die ("数据库连接失败：".mysql_error());
		mysql_select_db($this->table,$conn) or die ("没找到数据库：".$this->table);
		mysql_query("SET NAMES 'UTF8'");
	}

	function query($sql){
		$query = mysql_query($sql) or die ("执行失败：".mysql_error());
		return $query;
	}
	
	function num_rows($query){
		return mysql_num_rows($query);
	}

	function fetch_array($query){
		return mysql_fetch_array($query);
	}
	
	function fetch_object($query){
		return mysql_fetch_object($query);
	}
	
	function query_all($tablename , $limit = "1"){
		if($limit == "1"):return $this->query("SELECT * FROM 'tablename'");
		else:return $this->query("SELECT * FROM $tablename WHERE '$limit'");
		endif;
	}
	
	function fn_insert($tablename,$name,$value){
		$this->query("INSERT INTO $tablename ($name) VALUES ($value)");
	}

	function realip(){
		if(isset($_SERVER)):
			if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) : $realip = $_SERVER['HTTP_X_FORWARDER_FPR'];
			elseif(isset($_SERVER['HTTP_CLIENT_IP'])) : $realip = $_SERVER['HTTP_CLIENT_IP'];
			else : $realip = $_SERVER['REMOTE_ADDR'];
			endif;
		else:
			if(getenv("HTTP_X_FORWARDED_FOR")) : $realip = getenv("HTTP_X_FORWARDER_FPR");
			elseif(getenv("HTTP_CLIENT_IP")) : $realip = getenv("HTTP_CLIENT_IP");
			else : $realip = getenv("REMOTE_ADDR");
			endif;
		endif;
		return $realip;
	}
	
	function is_injection($content){
		return preg_match('select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|<\/script>', $sql_str);     // 进行过滤
	}
	
}
 
 
 
 
 
?>